#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using nHydrate.Generator.Common.EventArgs;

namespace nHydrate.Generator.ModelUI
{
	public partial class StatisticsForm : Form
	{
		public StatisticsForm()
		{
			InitializeComponent();
			this.IsExpanded = false;
			lblHeader.Text = "These files were found in folders with generated files, however they were not part of the generation. They are most likely old files that need to be deleted. Please verify that you wish to remove these files.";
		}

		#region Class Members

		private bool _isExpanded = false;
		private List<ProjectItemGeneratedEventArgs> _generatedFileList = new List<ProjectItemGeneratedEventArgs>();

		#endregion

		#region Property Implementations

		public bool IsExpanded
		{
			get { return _isExpanded; }
			set
			{
				_isExpanded = value;

				if (this.IsExpanded)
				{
					//Expand
					this.FormBorderStyle = FormBorderStyle.Sizable;
					this.Size = new Size(this.MinimumSize.Width, this.MinimumSize.Height * 2);
					this.cmdDetails.Text = "<< Details";
				}
				else
				{
					//Shrink
					this.FormBorderStyle = FormBorderStyle.FixedDialog;
					this.Size = this.MinimumSize;
					this.cmdDetails.Text = "Details >>";
				}

			}
		}

		public string DisplayText
		{
			get { return txtStats.Text; }
			set { txtStats.Text = value; }
		}

		public List<ProjectItemGeneratedEventArgs> GeneratedFileList
		{
			get { return _generatedFileList; }
			set
			{
				try
				{
					_generatedFileList = value;
					if (_generatedFileList == null)
						_generatedFileList = new List<ProjectItemGeneratedEventArgs>();
					lstFile.Items.Clear();

					//Load the parent folders
					var folderList = new ArrayList();
					foreach (var e in this.GeneratedFileList)
					{
						var fileName = e.FullName;
						if (!string.IsNullOrEmpty(fileName))
						{
							var fi = new FileInfo(fileName);
							if (!folderList.Contains(fi.DirectoryName))
								folderList.Add(fi.DirectoryName);
						}
					}

					//Now we have a folder list so load all files in all folders
					foreach (string folderName in folderList)
					{
						var di = new DirectoryInfo(folderName);
						var fileList = di.GetFiles("*.*");
						foreach (var fi in fileList)
						{
							//Skip the projects
							if ((fi.Extension.ToLower() != ".csproj") &&
								(fi.Extension.ToLower() != ".scc") &&
								(fi.Extension.ToLower() != ".vssscc") &&
								(!fi.FullName.ToLower().Contains(".csproj.")))
							{
								lstFile.Items.Add(fi.FullName);
							}
						}
					}

					//Now loop and remove the items that were generated
					foreach (var e in this.GeneratedFileList)
					{
						var fileName = e.FullName;
						if (!string.IsNullOrEmpty(fileName))
						{
							var fi = new FileInfo(fileName);
							lstFile.Items.Remove(fi.FullName);
						}
					}

				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
		}

		#endregion

		#region Button Handlers

		private void cmdDetails_Click(object sender, EventArgs e)
		{
			this.IsExpanded = !this.IsExpanded;
		}

		private void cmdOK_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void cmdDelete_Click(object sender, EventArgs e)
		{
			if (MessageBox.Show("Do you wish to delete all checked files?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
			{
				try
				{
					var sortedList = new SortedDictionary<string, ProjectItemGeneratedEventArgs>();
					foreach (var item in this.GeneratedFileList)
					{
						sortedList.Add(item.FullName, item);
					}

					var indexes = new List<int>();
					for (var ii = 0; ii < lstFile.Items.Count; ii++)
					{
						if (this.lstFile.GetItemChecked(ii))
						{
							var fileName = (string)lstFile.Items[ii];
							indexes.Add(ii);
							var fi = new FileInfo(fileName);
							fi.Attributes = FileAttributes.Normal;
							fi.Delete();
						}
					}

					//Remove the deleted file entry
					for (var jj = indexes.Count - 1; jj >= 0; jj--)
						lstFile.Items.RemoveAt((int)indexes[jj]);

				}
				catch (Exception ex)
				{
					throw ex;
				}
			}
		}

		private void cmdCheckAll_Click(object sender, EventArgs e)
		{
			for (var ii = 0; ii < lstFile.Items.Count; ii++)
				lstFile.SetItemChecked(ii, true);
		}

		private void cmdUncheckAll_Click(object sender, EventArgs e)
		{
			for (var ii = 0; ii < lstFile.Items.Count; ii++)
				lstFile.SetItemChecked(ii, false);
		}

		#endregion

	}
}